﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Reflection;
using CSP.Common.Web.Factories;
using CSP.Common.Web.Providers;
using System.Configuration;
using CSP.Common.IProviders;
using CSP.Common.Factories;

namespace CSP.Common.Web
{
    public abstract class CSPPageBase : Page
    {
        /// <summary>
        /// Discuz根域名配置Key
        /// </summary>
        private const string DISCUZDOMAINURL = "DiscuzDomain.URL";


        /// <summary>
        /// Discuz根域名地址
        /// </summary>
        private string _discuzDomainURL;

        /// <summary>
        /// Discuz根域名地址
        /// </summary>
        protected string DiscuzDomainURL
        {
            get { return _discuzDomainURL; }
        }

        private ILoggerProvider _loggerProvider;

        /// <summary>
        /// 日志对象
        /// </summary>
        public ILoggerProvider LoggerProvider
        {
            get { return _loggerProvider; }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //初始化信息
            _discuzDomainURL = ConfigurationManager.AppSettings[DISCUZDOMAINURL].ToString();


            _loggerProvider = LoggerFactory.GetErrorLogger();

        }


        /// <summary>
        /// 获取请求参数名为Action的值
        /// </summary>
        public string AjaxAction
        {
            get
            {
                return this.Request["Action"];
            }
        }

        /// <summary>
        /// 调用AjaxAction同名的方法
        /// </summary>
        protected virtual void InvokeAction()
        {
            if (this.AjaxAction == null)
                return;
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(this.AjaxAction, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
            if (method != null)
                method.Invoke(this, null);
        }

        /// <summary>
        /// 获取本地字符串资源
        /// </summary>
        /// <param name="resourceKey"></param>
        /// <returns></returns>
        protected string GetLocalResource(string resourceKey)
        {
            return this.GetLocalResourceObject(resourceKey) as string;
        }

        /// <summary>
        /// 成功跳转页面
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="returnPath"></param>
        protected void SuccessfullRedirect(string msg, string returnPath)
        {

            this.Response.Redirect("~/Successfull.aspx?msg=" + Server.UrlEncode(msg) + "&returnPath=" + Server.UrlEncode(returnPath));
        }

        /// <summary>
        /// 失败跳转页面
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="returnPath"></param>
        protected void FailRedirect(string msg, string returnPath)
        {
            this.Response.Redirect("~/Fail.aspx?msg=" + Server.UrlEncode(msg) + "&returnPath=" + Server.UrlEncode(returnPath));
        }

        /// <summary>
        /// 弹出msg信息
        /// </summary>
        /// <param name="msg"></param>
        protected void Alert(string msg)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "MsgBox", "<script language=javascript>alert('" + msg + "');</script>");
        }
    }
}